﻿using Microsoft.Extensions.DependencyInjection;
using Modules.CheckInCount;
using Modules.Project;
using Pipelines.Sockets.Unofficial.Arenas;
using PmSoft;
using PmSoft.Exceptions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Modules.Person.PersonConstants;

namespace Modules.Reports;

/// <summary>
/// 项目班组按天统计服务
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class ReportProjectGroupDaysService
{
    private readonly ReportProjectGroupDaysRepository reportProjectGroupDaysRepository;
    private readonly ProjectGroupMemberService projectGroupMemberService;
    private readonly CheckInCountService checkInCountService;

    public ReportProjectGroupDaysService(ReportProjectGroupDaysRepository reportProjectGroupDaysRepository,
        ProjectGroupMemberService projectGroupMemberService,
        CheckInCountService checkInCountService)
    {
        this.reportProjectGroupDaysRepository = reportProjectGroupDaysRepository;
        this.projectGroupMemberService = projectGroupMemberService;
        this.checkInCountService = checkInCountService;
    }

    /// <summary>
    /// 获取指定日期没有生成报告的项目ID集合
    /// </summary>
    /// <param name="date"></param>
    /// <param name="limit"></param>
    /// <returns></returns>
    public IEnumerable<int> GetProjectIdsInNoUpdate(DateTime date, int limit = 100)
    {
        return reportProjectGroupDaysRepository.GetProjectIdsInNoUpdate(date, limit);
    }

    /// <summary>
    /// 创建报告
    /// </summary>
    /// <param name="projectId"></param>
    /// <param name="date"></param>
    public void Generate(int projectId, DateTime date)
    {
        ReportProjectGroupDays projectGroupDays = new()
        {
            ProjectId = projectId,
            Date = date.Date,
            CreateTime = DateTime.Now
        };
        var groupMembers = projectGroupMemberService.GetProjectGroupMembersByProjectId(projectId);

        projectGroupDays.NumberAttendees = groupMembers.Where(a => a.ProjectId == projectId && a.Status == 进出场状态.进场).Count();
        projectGroupDays.ActualAttendance = checkInCountService.GetCountPerday(CountTenantType.Project, CountType.PersonTime, date, projectId)?.StatisticsCount ?? 0;
        projectGroupDays.AbnormalNumberPeople = Math.Abs(projectGroupDays.NumberAttendees - projectGroupDays.ActualAttendance);
        Create(projectGroupDays);

    }

    /// <summary>
    /// 项目班组按天统计表单详情
    /// </summary>
    /// <param name="id">主键</param>
    /// <returns></returns>
    public ReportProjectGroupDays? Get(int id)
    {
        return reportProjectGroupDaysRepository.Get(id);
    }

    /// <summary>
    /// 批量删除，根据id
    /// </summary>
    /// <param name="ids">id的集合</param>
    public void Deleted(IEnumerable<int> ids)
    {
        foreach (int id in ids)
        {
            Delete(id);
        }
    }

    /// <summary>
    /// 单个删除
    /// </summary>
    /// <param name="id">主键</param>
    public void Delete(int id)
    {
        reportProjectGroupDaysRepository.DeleteByEntityId(id);
    }

    /// <summary>
    /// 创建项目班组按天统计
    /// </summary>
    /// <param name="entity">实体对象</param>
    public void Create(ReportProjectGroupDays entity)
    {
        reportProjectGroupDaysRepository.Insert(entity);
    }

    /// <summary>
    /// 编辑项目班组按天统计
    /// </summary>
    /// <param name="entity">实体对象</param>
    public void Update(ReportProjectGroupDays entity)
    {
        reportProjectGroupDaysRepository.Update(entity);
    }

    /// <summary>
    /// </summary>
    /// <param name="query"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    public IPagedList<ReportProjectGroupDays> GetReportProjectDays(ReportProjectGroupDaysQuery query, int pageIndex, int pageSize)
    {
        return reportProjectGroupDaysRepository.GetReportProjectDays(query, pageIndex, pageSize);
    }

    /// <summary>
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    public IEnumerable<ReportProjectGroupDays> GetReportProjectDays(ReportProjectGroupDaysQuery query)
    {
        return reportProjectGroupDaysRepository.GetReportProjectDays(query);
    }
}